package com.zklh.leetcode.interviewTop150;

import com.zklh.leetcode.tree.model.TreeNode;

/**
 * 求根节点到叶节点数字之和
 * author: tongnan
 * date: 2024/7/26
 */
public class Solution129 {
    private int sum = 0;
    public int sumNumbers(TreeNode root) {
        sum = 0;
        dfs(root, 0);
        return sum;
    }

    public void dfs(TreeNode root, int traceNum) {
        if (root.right == null && root.left == null) {
            sum += traceNum * 10 + root.val;
            return;
        }
        if (root.left != null) {
            dfs(root.left, traceNum * 10 + root.val);
        }

        if (root.right != null) {
            dfs(root.right, traceNum * 10 + root.val);
        }
    }
}
